<?php
declare (strict_types=1);

namespace app\api\controller\seckill;

use app\api\controller\Base;
use app\common\service\BaseService;
use app\common\service\store\SeckillService;
use app\validate\SeckillCheck;
use think\Request;

class Seckill extends Base
{


    public function init(Request $request)
    {
      $seckillId = $request->get('seckill_id');
      SeckillService::initSeckill($seckillId);
    }
    /**
     * Author:YangXinPeng
     * 秒杀
     * @param Request $request
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function doAction(Request $request)
    {
        $params = $request->get();
        $this->validateFail(SeckillCheck::class, $params);
        $configId = $params['seckill_id'];
        $uid = $request->uid;
        $goodsId = $params['sku_id'];
        $info = SeckillService::seckill($configId, $uid, $goodsId);
        return $this->res($info);
    }

    /**
     * Author:YangXinPeng
     * 秒杀前台展示
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function showSeckill()
    {
        $info = SeckillService::show();
        return $this->res($info);
    }

    /**
     * Author:Yingbo Sun
     * 秒杀时间段展示
     */
    public function seckillList()
    {
        $time = $this->request->get('time', '0');
        if (empty($time)) {
            return $this->apiFail('参数错误');
        }
        switch ($time) {
            case "16:00":
                $timeQuantum = strtotime(date('Y-m-d 16:00:00'));
                $timeQuantums = strtotime(date('Y-m-d 17:59:59'));
                break;
            case "18:00":
                $timeQuantum = strtotime(date('Y-m-d 18:00:00'));
                $timeQuantums = strtotime(date('Y-m-d 19:59:59'));
                break;
            case "20:00":
                $timeQuantum = strtotime(date('Y-m-d 20:00:00'));
                $timeQuantums = strtotime(date('Y-m-d 21:59:59'));
                break;
            case "22:00":
                $timeQuantum = strtotime(date('Y-m-d 22:00:00'));
                $timeQuantums = strtotime(date('Y-m-d 23:59:59'));
                break;
            case "00:00":
                $timeQuantum = strtotime(date('Y-m-d 00:00:00'));
                $timeQuantums = strtotime(date('Y-m-d 1:59:59'));
                break;
        }
        $where[] = ['start_time', '>=', $timeQuantum];
        $where[] = ['end_time', '<=', $timeQuantums];
        $data = SeckillService::seckillList($where);
        return $this->res($data);

    }

    public function getSeckillDetail(Request $request)
    {
        $skuId = $request->get('sku_id');
        $seckillId = $request->get('seckill_id');
        $where = [];
        $where['goods_id'] = $skuId;
        $where['config_id'] = $seckillId;
        $data = \app\admin\model\store\Seckill::with('config')->where($where)->select();
        $data && $data = $data->toArray();
        return json(['code'=>200,'msg'=>'成功','data'=>$data]);
    }

}
